package com.lcf.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.lcf.framework.utils.UserUtils;
import com.lcf.framework.vo.PageVO;
import com.lcf.system.dao.GoodsCommentDao;
import com.lcf.system.dto.GoodsCommentDTO;
import com.lcf.system.entity.GoodsComment;
import com.lcf.system.query.GoodsCommentPageQuery;
import com.lcf.system.service.GoodsCommentService;
import com.lcf.system.vo.GoodsCommentVO;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * 商品评价(GoodsComment)表服务实现类
 *
 * @author lcf
 * @since 2025-05-02 11:55:01
 */
@Service("goodsCommentService")
public class GoodsCommentServiceImpl extends ServiceImpl<GoodsCommentDao, GoodsComment> implements GoodsCommentService {

    @Override
    public boolean addGoodsComment(GoodsCommentDTO dto) {
        GoodsComment goodsComment = BeanUtil.copyProperties(dto, GoodsComment.class);
        goodsComment.setCommentTime(LocalDate.now());
        goodsComment.setUserId(UserUtils.getUserId());
        return goodsComment.insert();
    }

    @Override
    public boolean deleteGoodsComment(long id) {
        return removeById(id);
    }

    @Override
    public boolean updateGoodsComment(long id, GoodsCommentDTO dto) {
        GoodsComment goodsComment = BeanUtil.copyProperties(dto, GoodsComment.class);
        goodsComment.setId(id);
        return goodsComment.updateById();
    }

    @Override
    public GoodsCommentVO getGoodsComment(long id) {
        GoodsComment comment = getById(id);
        return BeanUtil.copyProperties(comment, GoodsCommentVO.class);
    }

    @Override
    public PageVO<GoodsCommentVO> getGoodsCommentList(GoodsCommentPageQuery query) {
        Page<GoodsComment> page = Db.lambdaQuery(GoodsComment.class)
                .eq(GoodsComment::getGoodsId, query.getGoodsId())
                .orderByDesc(GoodsComment::getCreateTime)
                .page(query.toPage());
        return new PageVO<>(page, GoodsCommentVO.class);
    }
}

